package com.biannuo.szk.user.controller;

import com.biannuo.szk.user.domain.UserDemo;
import com.biannuo.szk.user.mapper.UserDemoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 *
 * @author szk
 */
@RestController
@RequestMapping("/api")
public class UserDemoController {
    /**
     * 模拟用户数据
     */
    private final Map<Integer, String> userInfo = new HashMap<Integer, String>() {{
        put(1, "ZhangSan");
        put(1024, "LiSi");
    }};
    @Value("${custom.info}")
    private String config;

    @Autowired
    private UserDemoMapper userDemoMapper;

    @RequestMapping("/users/findAll")
    public List<UserDemo> findAll() {
        return userDemoMapper.findAll();
    }

    /**
     * 根据id查询用户信息的方法
     *
     * @param id
     * @return
     */
    @RequestMapping("/users/findById")
    public String findById(@RequestParam("id") Integer id) {
        return userInfo.getOrDefault(id, null);
    }

    /**
     * 根据id查询用户信息的方法
     * 为了测试限流，API分组新增的方法
     *
     * @param id
     * @return
     */
    @RequestMapping("/user/group/findById")
    public String groupFindById(@RequestParam("id") Integer id) {
        return userInfo.getOrDefault(id, null);
    }


    @RequestMapping("/users/getConfig")
    public String getConfig() {  // 获取配置的接口
        return config;
    }

}